package
{
	import com.adamatomic.flixel.*;

	public class MenuState extends FlxState
	{
		[Embed(source="../data/cursor.png")] private var ImgCursor:Class;
		[Embed(source="../data/menu_hit.mp3")] private var SndHit:Class;
		[Embed(source="../data/menu_hit_2.mp3")] private var SndHit2:Class;
		
		private var _e:FlxEmitter;
		private var _t1:FlxText;
		private var _t2:FlxText;
		private var _ok:Boolean;
		
		override public function MenuState():void
		{
			var i:uint;
			var a:FlxArray = new FlxArray();
			for(i = 0; i < 2000; i++)
			{
				if(i%3)
					this.add(a.add(new FlxSprite(null,0,0,false,false,16,16,0xff3a5c39)) as FlxSprite);
				else
					this.add(a.add(new FlxSprite(null,0,0,false,false,2,2,0xffd8eba2)) as FlxSprite);
			}
			_e = new FlxEmitter(FlxG.width/2-50,FlxG.height/2-10,100,30,a,-5,-100,100,-800,-100,0,0,400);
			_e.kill();
			this.add(_e);
				
			_t1 = this.add(new FlxText(FlxG.width,FlxG.height/3,200,80,"sudoku",0x3a5c39,null,32)) as FlxText;
			_t2 = this.add(new FlxText(-60,FlxG.height/3,200,80,"solver",0x3a5c39,null,32)) as FlxText;
			
			_ok = false;
			
			FlxG.setCursor(ImgCursor);
		}

		override public function update():void
		{
			//Slides the text ontot he screen
			var t1m:uint = FlxG.width/2-150;
			if(_t1.x > t1m)
			{
				_t1.x -= FlxG.elapsed*FlxG.width;
				if(_t1.x < t1m) _t1.x = t1m;
			}
			var t2m:uint = FlxG.width/2+6;
			if(_t2.x < t2m)
			{
				_t2.x += FlxG.elapsed*FlxG.width;
				if(_t2.x > t2m) _t2.x = t2m;
			}
			
			//Check to see if the text is in position
			if(!_ok && ((_t1.x == t1m) || (_t2.x == t2m)))
			{
				//explosion
				_ok = true;
				FlxG.play(SndHit);
				FlxG.flash(0xffd8eba2,0.5);
				FlxG.quake(0.035,0.5);
				_t1.setColor(0xd8eba2);
				_t2.setColor(0xd8eba2);
				_e.reset();
				_t1.angle = Math.random()*40-20;
				_t2.angle = Math.random()*40-20;
				
				t1m = FlxG.width/2-54;
				this.add(new FlxText(t1m,FlxG.height/3+39,110,20,"by Matt Hughson",0x3a5c39,null,8,"center"));
				
				//flixel button
				this.add(new FlxSprite(null,t1m+1,FlxG.height/3+53,false,false,106,19,0xff131c1b));
				this.add(new FlxButton(t1m+2,FlxG.height/3+54,new FlxSprite(null,0,0,false,false,104,15,0xff3a5c39),onFlixel,new FlxSprite(null,0,0,false,false,104,15,0xff729954),new FlxText(15,1,100,10,"www.flixel.org",0x729954),new FlxText(15,1,100,10,"www.flixel.org",0xd8eba2)));
				
				//danny B button
				this.add(new FlxSprite(null,t1m+1,FlxG.height/3+75,false,false,106,19,0xff131c1b));
				this.add(new FlxButton(t1m+2,FlxG.height/3+76,new FlxSprite(null,0,0,false,false,104,15,0xff3a5c39),onMatthughson,new FlxSprite(null,0,0,false,false,104,15,0xff729954),new FlxText(7,1,100,20,"matthughson.com",0x729954),new FlxText(7,1,100,20,"matthughson.com",0xd8eba2)));
				
				//play button
				this.add(new FlxSprite(null,t1m+1,FlxG.height/3+137,false,false,106,19,0xff131c1b));
				this.add(new FlxButton(t1m+2,FlxG.height/3+138,new FlxSprite(null,0,0,false,false,104,15,0xff3a5c39),onButton,new FlxSprite(null,0,0,false,false,104,15,0xff729954),new FlxText(25,1,100,10,"CLICK HERE",0x729954),new FlxText(25,1,100,10,"CLICK HERE",0xd8eba2))) as FlxButton;
			}

			super.update();
		}
		
		private function onFlixel():void
		{
			FlxG.openURL("http://flixel.org");
		}
		
		private function onMatthughson():void
		{
			FlxG.openURL("http://matthughson.com");
		}
		
		private function onButton():void
		{
			FlxG.flash(0xffd8eba2,0.5);
			FlxG.fade(0xff131c1b,1,onFade);
			FlxG.play(SndHit2);
		}
		
		private function onFade():void
		{
			FlxG.switchState( GameState );
		}
	}
}

// Testing remote submission 02
// My local change
